package com.vichat.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

/**
 * Created by wangluyin on 2018/7/16.
 */
public class I18NUtil {
    protected static final Logger logger = LoggerFactory.getLogger(I18NUtil.class);
    private static ApplicationContext ctx;

    public static ApplicationContext getApplicationContext(){

        return ctx;
    }

    public static void setApplicationContext(ApplicationContext context) throws BeansException {
        ctx = context;
    }

    public static String getMessage(String key,Object[] var2,Locale locale){
       String message =  ctx.getMessage( key,  var2,locale);
        logger.info(key+":"+message);
       return  message;
    }

    public static String getMessage(String key, Object[] var2, HttpServletRequest request){
        Object sessionLocale = request.getSession().getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
        Locale locale = null;
        if(sessionLocale!=null){
            locale = (Locale)sessionLocale;
        }else{
            locale = Locale.getDefault();
        }
        return  ctx.getMessage( key,  var2,locale);
    }
}
